package com.quick.user.controller;

import com.quick.common.Constent.Quick;
import com.quick.common.bean.User;
import com.quick.common.dto.*;
import com.quick.common.service.IUserService;
import com.quick.common.util.ImageUtil;
import com.quick.common.util.Result;
import com.quick.common.util.TokenUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @DubboReference
    private IUserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("login")
    public Result<String> login(@RequestBody LoginDTO loginDTO) {
        String token = userService.userLogin(loginDTO);
        stringRedisTemplate.opsForValue().set("user_token",token);
        return Result.success("登陆成功",token);
    }
    @GetMapping("getUserbean/{id}")
    public Result<Object> getUserbean(@PathVariable("id") Integer id) {
        System.out.println(userService.getUserLoginBeanById(id));
        return Result.success("查询成功",userService.getUserLoginBeanById(id));
    }

    @PostMapping("register")
    public Result<String> register(@RequestBody RegisterDTO registerDTO) {
        userService.userRegister(registerDTO);
        return Result.success("用户注册成功");
    }
    @GetMapping("getTokenInfo")
    public Result<TokenInfoDTO> getTokenInfo(@RequestHeader("Authorization") String authorizationHeader){
        System.out.println("2111111111111121"+authorizationHeader);
        String token;
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            token = authorizationHeader.substring(7);
        } else {
            token = authorizationHeader;
        }
        String s = TokenUtil.getInfo(token, Quick.TOKEN_KEY_ID);
        System.out.println("ID"+s);
        int id = Integer.parseInt(s);
        String role = TokenUtil.getInfo(token, Quick.TOKEN_KEY_ROLE);
        System.out.println("ROLE"+role);
//        String name = TokenUtil.getInfo(token, Quick.TOKEN_KEY_NAME);
//        System.out.println("NAME"+name);
        TokenInfoDTO tokenInfo = new TokenInfoDTO();
        tokenInfo.setId(id);
        tokenInfo.setRole(role);
        System.out.println("token"+tokenInfo);
        return Result.success("",tokenInfo);
    }
    @PostMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            InputStream is=file.getInputStream();
            String filename=file.getOriginalFilename();
            String newFileName = ImageUtil.upload(is,filename);
            return Result.success("图片上传成功",newFileName);
        } catch (IOException e) {
            throw new RuntimeException("图片上传失败");
        }
    }
    @PostMapping("upload")
    public Result<String> editImg(@RequestBody ImageDTO imageDTO){
            String img = userService.upload(imageDTO);
            System.out.println("img"+img);
            return Result.success(img);
    }

    @PutMapping("editUser")
    public Result<Integer> editUser(@RequestBody User user){
        return Result.success("修改成功",userService.editUser(user));
    }

    @PutMapping("changePassword")
    public Result changePassword(@RequestBody PasswordDTO passwordDTO){
        try{
            if (!Objects.equals(passwordDTO.getNewPassword(),passwordDTO.getConfirmPassword())){
                return Result.error("两次密码不一致");
            }
            System.out.println("passwordDTO"+passwordDTO);
            userService.changePassword(passwordDTO);
            return Result.success("修改成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
